package com.netty.cn.rpc.jboss.server;

import com.netty.cn.rpc.jboss.model.SubscribeReq;
import com.netty.cn.rpc.jboss.model.SubscribeResp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 
 * @author wangzonghui
 * @date 2021-07-20 10:04:37
 * @Description 服务端处理类
 */
public class SubReqServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        SubscribeReq req = (SubscribeReq) msg;
        if ("LMRZero".equalsIgnoreCase(req.getUserName())) {
            System.out.println("服务端接收到客户端消息为 : [" + req.toString() + "]");
            ctx.writeAndFlush(resp(req.getSubReqID()));
        }
    }
	
    /**
     * 服务端消息封装方法
     * @param subReqID
     * @return
     */
    private SubscribeResp resp(int subReqID) {
        SubscribeResp resp = new SubscribeResp();
        resp.setSubReqID(subReqID);
        resp.setRespCode(0);
        resp.setDesc("接收到订单，快递将在3天后到达");
        return resp;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{
        cause.printStackTrace();
        ctx.close();// 发生异常，关闭链路
    }
}
